//
//  LoginViewController.m
//  ReactiveObjCDemo
//
//  Created by xth on 2017/12/9.
//  Copyright © 2017年 xth. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginModel.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;

@property (weak, nonatomic) IBOutlet UITextField *accountTF;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (nonatomic, strong) LoginModel *loginModel;

@end

@implementation LoginViewController


/*
 需求：监听两个输入框的内容，有了内容才允许按钮点击
 
*/

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self bindModel];
}

//视图模型绑定
- (void)bindModel {
    
    RAC(self.loginModel.account, account) = _accountTF.rac_textSignal;
    RAC(self.loginModel.account, pwd) = _pwdTF.rac_textSignal;
    
    RAC(self, self.loginButton.enabled) = self.loginModel.enableLoginSignal;
    
    //监听登录按钮点击
    [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

        //执行登录事件
        [self.loginModel.LoginCommand execute:nil];
    }];
}



- (LoginModel *)loginModel {
    if (!_loginModel) {
        _loginModel = [[LoginModel alloc] init];
    }
    return _loginModel;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
